/**
 * 管理后台通用工具库
 * 提供消息提示、确认对话框、表单验证等通用功能
 */

// 消息提示系统
class NotificationManager {
    constructor() {
        this.container = null;
        this.init();
    }

    init() {
        // 创建消息容器
        this.container = document.getElementById('notification-container');
        if (!this.container) {
            this.container = document.createElement('div');
            this.container.id = 'notification-container';
            this.container.style.cssText = 'position: fixed; top: 20px; right: 20px; z-index: 9999;';
            document.body.appendChild(this.container);
        }
        
        // 添加通知样式
        this.addStyles();
    }
    
    addStyles() {
        // 检查是否已经添加过样式
        if (document.getElementById('notification-styles')) {
            return;
        }
        
        const style = document.createElement('style');
        style.id = 'notification-styles';
        style.textContent = `
            .notification {
                background: white;
                border-radius: 8px;
                box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
                padding: 16px 20px;
                margin-bottom: 10px;
                min-width: 300px;
                max-width: 400px;
                position: relative;
                animation: slideIn 0.3s ease;
                border-left: 4px solid #3498db;
            }
            
            .notification-success {
                border-left-color: #27ae60;
                background: #d5f4e6;
            }
            
            .notification-error {
                border-left-color: #e74c3c;
                background: #fadbd8;
            }
            
            .notification-warning {
                border-left-color: #f39c12;
                background: #fef9e7;
            }
            
            .notification-info {
                border-left-color: #3498db;
                background: #ebf3fd;
            }
            
            .notification-close {
                position: absolute;
                top: 8px;
                right: 12px;
                background: none;
                border: none;
                font-size: 18px;
                cursor: pointer;
                color: #666;
                padding: 0;
                width: 20px;
                height: 20px;
                display: flex;
                align-items: center;
                justify-content: center;
            }
            
            .notification-close:hover {
                color: #333;
            }
            
            @keyframes slideIn {
                from {
                    transform: translateX(100%);
                    opacity: 0;
                }
                to {
                    transform: translateX(0);
                    opacity: 1;
                }
            }
            
            @keyframes slideOut {
                from {
                    transform: translateX(0);
                    opacity: 1;
                }
                to {
                    transform: translateX(100%);
                    opacity: 0;
                }
            }
        `;
        document.head.appendChild(style);
    }

    show(message, type = 'info', duration = 3000) {
        const notification = document.createElement('div');
        notification.className = `notification notification-${type}`;
        
        // 存储定时器ID
        let autoRemoveTimer = null;
        
        // 创建关闭按钮
        const closeBtn = document.createElement('button');
        closeBtn.className = 'notification-close';
        closeBtn.innerHTML = '×';
        closeBtn.onclick = () => {
            if (autoRemoveTimer) {
                clearTimeout(autoRemoveTimer);
            }
            this.remove(notification);
        };
        
        // 设置内容
        notification.innerHTML = `<span>${message}</span>`;
        notification.appendChild(closeBtn);
        
        // 添加到容器
        this.container.appendChild(notification);
        
        // 自动移除
        if (duration > 0) {
            autoRemoveTimer = setTimeout(() => {
                this.remove(notification);
            }, duration);
        }
        
        return notification;
    }

    remove(notification) {
        if (!notification || !notification.parentNode) {
            return;
        }
        
        // 防止重复移除
        if (notification.dataset.removing) {
            return;
        }
        notification.dataset.removing = 'true';
        
        // 播放滑出动画
        notification.style.animation = 'slideOut 0.3s ease';
        
        // 动画完成后移除元素
        setTimeout(() => {
            if (notification && notification.parentNode) {
                notification.parentNode.removeChild(notification);
            }
        }, 300);
    }

    clearAll() {
        const notifications = this.container.querySelectorAll('.notification');
        notifications.forEach(notification => {
            this.remove(notification);
        });
    }

    forceClearAll() {
        this.container.innerHTML = '';
    }

    getCount() {
        return this.container.querySelectorAll('.notification').length;
    }
}

// 确认对话框系统
class ConfirmDialog {
    constructor() {
        this.overlay = null;
        this.callback = null;
        this.init();
    }

    init() {
        // 创建确认对话框HTML
        const html = `
            <div id="custom-confirm" class="custom-confirm-overlay" style="display: none;">
                <div class="custom-confirm-dialog">
                    <div class="custom-confirm-header">
                        <h3><i class="fas fa-exclamation-triangle"></i> 确认操作</h3>
                    </div>
                    <div class="custom-confirm-body">
                        <p id="confirm-message">您确定要执行此操作吗？</p>
                    </div>
                    <div class="custom-confirm-footer">
                        <button type="button" class="btn-confirm-cancel" id="btn-confirm-cancel">取消</button>
                        <button type="button" class="btn-confirm-ok" id="btn-confirm-ok">确定</button>
                    </div>
                </div>
            </div>
        `;
        
        // 添加到页面
        document.body.insertAdjacentHTML('beforeend', html);
        this.overlay = document.getElementById('custom-confirm');
        
        // 绑定事件
        this.bindEvents();
    }

    bindEvents() {
        const cancelBtn = document.getElementById('btn-confirm-cancel');
        const okBtn = document.getElementById('btn-confirm-ok');
        
        // 移除旧的事件监听器
        this.overlay.removeEventListener('click', this.handleOverlayClick);
        cancelBtn.removeEventListener('click', this.handleCancelClick);
        okBtn.removeEventListener('click', this.handleOkClick);
        
        // 添加新的事件监听器
        this.overlay.addEventListener('click', this.handleOverlayClick.bind(this));
        cancelBtn.addEventListener('click', this.handleCancelClick.bind(this));
        okBtn.addEventListener('click', this.handleOkClick.bind(this));
    }

    handleOverlayClick(e) {
        if (e.target === this.overlay) {
            this.hide();
        }
    }

    handleCancelClick() {
        this.hide();
    }

    handleOkClick() {
        if (this.callback) {
            this.callback();
        }
        this.hide();
    }

    show(message, callback) {
        document.getElementById('confirm-message').textContent = message;
        this.callback = callback;
        this.overlay.style.display = 'flex';
    }

    hide() {
        this.overlay.style.display = 'none';
        this.callback = null;
    }
}


// 侧边栏管理已移至 sidebar.html 中



// 全局实例
window.Utils = {
    notification: new NotificationManager(),
    confirm: new ConfirmDialog()
};

// 兼容性函数
window.showNotification = (message, type, duration) => {
    return window.Utils.notification.show(message, type, duration);
};

window.showCustomConfirm = (message, callback) => {
    return window.Utils.confirm.show(message, callback);
};

window.clearAllNotifications = () => {
    return window.Utils.notification.clearAll();
};

// 页面加载完成后初始化
document.addEventListener('DOMContentLoaded', function() {
    // 侧边栏管理已移至 sidebar.html 中
});


